package com.ebelter.temperature.models;


import android.os.Message;


import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.common.IOrder;
import com.ebelter.btcomlib.models.bluetooth.products.temperature.bean.TMesureResult;
import com.ebelter.btcomlib.models.bluetooth.products.temperature.bean.TMesureResult_Table;
import com.ebelter.btcomlib.models.https.IHttpRequestField;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.raizlabs.android.dbflow.sql.language.Select;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体温计的数据上传工具
 */

public class T_UploadUtil extends BaseHandle{


    private static final String TAG ="T_UploadUtil";

    private static final int WHAT_CHECK_DB_DATA = 1;
    private static final int UPLOAD_DATA_SUCCESS = 2;

    private IOrder mIOrder;

    private List<String> uploading_MesureTimes;

    public T_UploadUtil(IOrder mIOrder, Object... objects) {
        super(objects);
        this.mIOrder = mIOrder;
        uploading_MesureTimes = new ArrayList<>();
    }

    public void upload(Object tag, TMesureResult tMesureResult){
        upload(tag,tMesureResult,true);
    }

    private void upload(Object tag, final TMesureResult tMesureResult, final boolean newData){

        if (tMesureResult == null||tMesureResult.userID <= 0){
            return;
        }


        if (!NetUtils.available()){
            //网络不可用 先保存在数据库
            if (newData){
                LogUtils.i(TAG,"网络不可用,新的测量数据，保存在数据库");
                tMesureResult.save();
            }
            return;
        }

        if (uploading_MesureTimes.contains(tMesureResult.testTime)){
            LogUtils.i(TAG,"有相同时间的的测量数据 被屏蔽掉");
            return;
        }


        Map<String,Serializable> paramMap=new HashMap<>();
       // paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.USER_ID, tMesureResult.userID);
        paramMap.put(IHttpRequestField.temperature,tMesureResult.mesureValue);
        paramMap.put(IHttpRequestField.testDate,tMesureResult.testTime);

        uploading_MesureTimes.add(tMesureResult.testTime);


        NetUtils.getInstance().uploadMesureData_T(tag, paramMap, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {

                uploading_MesureTimes.remove(tMesureResult.testTime);

                if (!isRequestSuccess){
                    //上传失败的意思
                    if (newData){
                        tMesureResult.save();
                    }
                    LogUtils.i(TAG,"-----上传失败1----"+tMesureResult.toString());
                    return;
                }

                if (baseResponse.resultCode==0){

                    LogUtils.i(TAG,"-----上传成功----"+tMesureResult.toString());
                    //表示上传成功
                    if (!newData){
                        tMesureResult.delete();
                    }

                    if (mIOrder!=null)mIOrder.cmd(UPLOAD_DATA_SUCCESS,UPLOAD_DATA_SUCCESS);

                }else {
                    //表示上传失败
                    if (newData){
                        tMesureResult.save();
                    }
                    LogUtils.i(TAG,"-----上传失败2----"+tMesureResult.toString());
                }
            }
        });


    }



    public void startCheckDB(){
        removeMessages(WHAT_CHECK_DB_DATA);
        sendEmptyMessageDelayed(WHAT_CHECK_DB_DATA,10000);

        long userID=UserSpUtil.readLong(Constants.IUser.USER_ID);
        if (userID<=0){
            LogUtils.i(TAG,"---checkDbData---userID小于等于零 " + userID);
            return;
        }

        TMesureResult tMesureResult = new Select().from(TMesureResult.class).where(TMesureResult_Table.userID.eq(userID)).querySingle();
        if (tMesureResult!=null){
            LogUtils.i(TAG,"---数据库检测到未上传的数据，准备上传");
            upload(this,tMesureResult,false);
        }else {
            LogUtils.i(TAG,"---数据库未检测到缓存数据----");
        }

    }

    @Override
    public void handleMsg(Message msg) {
        super.handleMsg(msg);
        if (msg.what == WHAT_CHECK_DB_DATA){
            startCheckDB();
        }
    }

    public void exit(){
        removeCallbacksAndMessages();
        mIOrder=null;
        uploading_MesureTimes.clear();
    }
}
